package com.shsoft.utils;

import android.content.SharedPreferences;

import com.shsoft.common.Constant;
import com.shsoft.common.MyApplication;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static android.content.Context.MODE_PRIVATE;

/**
 * 操作SharedPreferences的工具类
 */
public class SharedPreferencesUtil {

    /**
     * 获取预设颜色,十六进制转颜色
     */
    public static Map<Integer, int[]> getColors(){
        Map<Integer, int[]> colors = new HashMap<>();
        SharedPreferences preferences = MyApplication.getContext().getSharedPreferences("colors", MODE_PRIVATE);
        String presetsColors = preferences.getString("presetsColors","");
        if(presetsColors.equals("")){
            return colors;
        }else {
//            MessageUtil.logDebug(presetsColors);
            for(int i = 0; i < presetsColors.length() / 6; i++){
                int r = HexUtil.hexStr2int( presetsColors.substring( i * 6, i * 6 + 2));
                int g = HexUtil.hexStr2int( presetsColors.substring( i * 6 + 2, i * 6 + 4));
                int b = HexUtil.hexStr2int( presetsColors.substring( i * 6 + 4, i * 6 + 6));
                colors.put(i, new int[]{r,g,b});
            }
            return colors;
        }
    }

    /**
     * 将预设颜色存入preferences
     */
    public static void setColors(Map<Integer, int[]> colors){
        SharedPreferences preferences = MyApplication.getContext().getSharedPreferences("colors", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < Constant.COLOR_COUNT; i++){
            if( colors.get(i) == null){ // 全部搞定
                break;
            }
            int[] ints = colors.get(i);
            sb.append(HexUtil.intStrToHexStr(ints[0]+ ""));
            sb.append(HexUtil.intStrToHexStr(ints[1]+ ""));
            sb.append(HexUtil.intStrToHexStr(ints[2]+ ""));
        }
        edit.putString("presetsColors", sb.toString());
        edit.commit();
    }

}
